package com.azkj.album.presenter;

import com.azkj.album.BuildConfig;
import com.azkj.album.dto.UpdateBean;
import com.azkj.album.network.NetworkMaster;
import com.azkj.album.network.callback.ServiceCallback;
import com.azkj.album.network.networkframe.bean.BaseResp;
import com.azkj.album.network.networkframe.net.exception.ApiException;
import com.azkj.album.network.utils.ToastUtils;
import com.azkj.album.view.base.BasePresenter;
import com.azkj.album.view.iview.IUpdateView;

public class UpdatePresenter extends BasePresenter {

    private IUpdateView iUpdateView;

    public UpdatePresenter(IUpdateView view) {
        iUpdateView = view;
    }

    /**
     * @param showDialog 当为最新版本时，是否弹窗。用于首页和个人中心区别
     *                   首页只toast,个人中心弹窗
     */
    public void checkVersion(boolean showDialog) {

        NetworkMaster.getInstance().getCommonService().checkVersion(new ServiceCallback<BaseResp<UpdateBean>>() {
            @Override
            public void onSuccess(BaseResp<UpdateBean> jsonObj) {

                if (jsonObj.getCode() == 1) {
                    if (jsonObj.getData() != null) {

                        if ((!showDialog && BuildConfig.VERSION_CODE >= jsonObj.getData().getAndroid_version())) {
                            ToastUtils.showCenterToast("当前版本为最新版本！");
                        } else {
                            iUpdateView.showUpdateDialog(jsonObj.getData());
                        }
                    } else {
                        ToastUtils.showCenterToast("当前版本为最新版本！");
                    }

                } else {
                    ToastUtils.showCenterToast(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                ToastUtils.showCenterToast(apiException.errorInfo.error);
            }

        });


    }

}
